<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Favorites extends MY_Controller {

    var $_templates = array();
    var $_login_in = array();
    function __construct() {
        parent::__construct();
        $this->session->set_userdata(array('Url' => uri_string()));
        if(!$this->logged_in()) {
            $this->session->set_userdata("redirect", urlAlias('danh-sach-ua-thich'));
            redirect(base_url()."login");
        }
        $this->_login_in = $this->session->userdata('logged_in');
    }

    public function index() {
        $data = array();
        if (!empty($this->_login_in)) {
            $userID = $this->_login_in['id'];
            $this->load->model('favorite_product_model');
            $listproductItems = $userID?$this->favorite_product_model->getProducts($userID):"";
            $this->_data['listproductItems'] = $listproductItems;
        }
        $this->_templates['page'] = 'favorites/index';
        $this->site_lib->load($this->_templates['page'], $this->_data);
    }

    public function add_product() {
        $json = array();

        
        if (!empty($this->_login_in)) {
            // Save san pham
            $this->load->model('favorite_product_model');
            $productId = $this->input->post('product_id');
            $userID = $this->_login_in['id'];
            $favorite = $this->favorite_product_model->getFavorite($productId, $userID);
            if (empty($favorite)) {
                $status = $this->favorite_product_model->saveData(0, array(
                    'user_id' => $userID,
                    'product_id' => $productId
                    )
                );
                if ($status) {
                    $json['status'] = true;
                    $json['message'] = "Sản phẩm đã được đưa vào danh sách ưa thích!";
                }
            } else {
                $json['status'] = true;
                $json['message'] = "Bạn đã chọn yêu thích sản phẩm rồi!";
            }
        } else {
            $json['status'] = true;
            $json['message'] = "Bạn phải login để thực hiện chức năng này";
        }
        echo json_encode($json);
        exit();
    }

    public function delete_product($productId = 0) {
        $json = array();

        $loggedin = $this->session->userdata('logged_in');
        if (!empty($loggedin) && $productId) {
            // Save san pham
            $this->load->model('favorite_product_model');
            $userID = $loggedin['id'];
            $favorite = $this->favorite_product_model->getFavorite($productId, $userID);
            if (!empty($favorite)) {
                $status = $this->favorite_product_model->delete($favorite->favorite_product_id);
            } 
        }
        redirect(urlAlias('danh-sach-ua-thich'));
    }

}

/* End of file favorites.php */
/* Location: ./application/site/controllers/favorites.php */